package balance

import (
	"math/rand"
	"net/http"
	"time"
)

// 随机策略
func (service *Service) randomBalance(req *http.Request) (index int, err error) {
	service.Lock.Lock()
	defer service.Lock.Unlock()

	index = service.randNum(service.CountInfo.Len)

	return
}

func (service *Service) randNum(max int) int {
	r := rand.New(rand.NewSource(service.getRandSeek(&service.CountInfo.RandSeek)))
	return r.Intn(max)
}

// 纳秒作为随机种子，再加一个randseek
func (service *Service) getRandSeek(randSeek *int64) int64 {
	if *randSeek >= 1000 {
		*randSeek = 0
	}
	*randSeek++

	return time.Now().UnixNano() + *randSeek
}
